<?php
/**
 *
 * User: lang@vip.deyi.com
 * Date: 2018/1/8
 * Time: 11:10
 */

namespace Apps\Weixin\Models;


use Apps\Common\Model\BaseModel;

class WxConfig extends BaseModel
{
    // 定义此model访问的数据库名
    protected static $_DBNAME = 'yyx_fanli';

    // 定义此model访问的表名
    protected static $_TABLENAME = 'wx_config';

    public function getAttrListByType($type)
    {
        $sql = 'SELECT attrname,attrval FROM ' . self::$_TABLENAME . ' WHERE attrtype=? AND enable=1';
        $res = $this->selectCols($sql, ['attrname', 'attrval'], [$type]);

        $data = [];

        foreach ($res as $v) {
            $data[$v->attrname] = $v->attrval;
        }

        return $data;
    }


    public function getListByConditions($conditions, $offset, $limit, $order = ' id DESC ')
    {
        list($values, $conds, $likeConds) = $this->assembleConditionsData($conditions);
        $where = $this->assembleComplexWhereSql($conds, $likeConds);

        $sql = 'SELECT * FROM ' . self::$_TABLENAME . $where . ' ORDER BY ' . $order . ' ' . $this->assembleLimit($offset, $limit);

        return $this->select($sql, $values);
    }

    public function countListByConditions($conditions)
    {
        list($values, $conds, $likeConds) = $this->assembleConditionsData($conditions);
        $where = $this->assembleComplexWhereSql($conds, $likeConds);

        $sql = 'SELECT COUNT(*) AS num FROM ' . self::$_TABLENAME . $where;

        return $this->selectCount($sql, $values);
    }

    public function getConfigDataByService($service)
    {
        $sql = 'SELECT * FROM ' . self::$_TABLENAME . ' WHERE service=? AND enable=1';

        return $this->select($sql, [$service]);
    }
}